package com.yunhe.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yunhe.bean.ShopImfor;
import com.yunhe.service.GoodsBoardService;

/**
 * Servlet implementation class showGoodList
 */
/*@WebServlet(name="showGoodList.do", urlPatterns="showGoodList.do")  */
@WebServlet("/showGoodList.do")
public class showGoodList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showGoodList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String sid = request.getParameter("sid");
		if(sid != null) {
			List<ShopImfor> list = new GoodsBoardService().getGoodsList(Integer.parseInt(sid));
			session.setAttribute("list", list);
			request.getRequestDispatcher("hou/goods-list.jsp").forward(request, response);
		}else {
			String page = request.getParameter("page");//页码
			int i = 0;
			if(page == null) {
				i=0;//第一页
			}else {
				i = Integer.parseInt(page);//所在页
			}
			List<ShopImfor> list = new GoodsBoardService().getSeparaGoodsList(i*5);//将每页的数据存在一个集合中
			int sum = new GoodsBoardService().getGoods().size();//数据总条数
			int sumPage = sum%5==0?sum/5:sum/5+1;//总页数	
			session.setAttribute("sum", sum);
			session.setAttribute("i", page);
			session.setAttribute("sumPage", sumPage-1);
			session.setAttribute("list", list);
			//request.getRequestDispatcher("hou/goods-list.jsp").forward(request, response);
			response.sendRedirect("hou/goods-list.jsp");
		}
		
		
		
	}

}
